﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Operator
{
    /// <summary>
    /// OperatorUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class OperatorUserControl : UserControl, IPlugin
    {

        private WarehouseService warehouseService = ServiceContainer.GetService<WarehouseService>();
        private OperatorService operatorService = ServiceContainer.GetService<OperatorService>();

        public OperatorUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return " 操作员管理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.warehouseService.GetPageByMap(new Dictionary<string, object>(), 0, 0).Datas;
                item.Insert(0, new ProxyDelivery.Domain.Warehouse { Name = "所有", Id = 0 });
                this.cbbWarehouse.ItemsSource = item;
                this.cbbWarehouse.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.pbBar.Parameters.Clear();
            this.pbBar.Parameters.Add("number", this.tbNumber.Text.Trim());
            this.pbBar.Parameters.Add("name", this.tbName.Text.Trim());
            this.pbBar.Parameters.Add("phone", this.tbPhone.Text.Trim());
            this.pbBar.Parameters.Add("warehouse", this.cbbWarehouse.SelectedIndex);
            this.pbBar.StartPage();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var op = this.dgvOperators.SelectedItem as Mengsk.ProxyDelivery.Domain.Operator;
                if (op == null)
                {
                    throw new Exception("请选择操作员");
                }
                new OperatorEditWindow { Operator = op }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                new OperatorEditWindow().ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var dataCollection = this.operatorService.GetByAll(e.GetParameter<string>("number"), e.GetParameter<string>("name"), e.GetParameter<string>("phone"), e.GetParameter<int>("warehouse"), e.CurrentPage - 1, e.PageSize);
            this.dgvOperators.ItemsSource = dataCollection.Datas;
            this.pbBar.Total = dataCollection.Total;
            this.pbBar.CurrentCount = dataCollection.Datas.Count;
        }

        private void btnResetPassword_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var op = this.dgvOperators.SelectedItem as Mengsk.ProxyDelivery.Domain.Operator;
                if (op == null)
                {
                    throw new Exception("请选择操作员");
                }
                new ResetPasswordWindow { Operator = op }.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
